package com.axjy.auth.service.impl;

import com.axjy.auth.feign.UserFeignClient;
import com.axjy.auth.service.UserService;
import com.axjy.auth.uitl.TokenUtil;
import com.axjy.commons.pojo.TpUser;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserFeignClient userFeignClient;

    @Resource
    private TokenUtil tokenUtil;


    @Resource
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public Map login(String phone, String password) {
        TpUser tpUser=userFeignClient.find(phone,password);
        if(tpUser==null){
            throw new RuntimeException("用户名或密码错误");
        }

        Map map=new HashMap();
        map.put("loginUser",tpUser);
        String token=tokenUtil.createToken(tpUser.getId());
        map.put("token",token);

        ValueOperations operations= stringRedisTemplate.opsForValue();
        operations.set("token:"+Long.toString(tpUser.getId()),token,20, TimeUnit.MINUTES);


        return map;
    }
}
